<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%@ page import="db.HistoryUnit" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "index.jsp";
        response.sendRedirect(redirectURL);
    }
    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
%>

<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 07.05.12
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Account history</title>
    <link rel="stylesheet" type="text/css" href="style"/>
 </head>
<body>
<div class="logout" style="text-align:right;"><a href="logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><b><a href="accounts.jsp">Accounts</a></b></li>
            <li><a href="operations.jsp">Operations</a></li>
            <li><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></li>
        </ul>
    </div>
    <div class="content">
        <%
            String dateFrom = request.getParameter("date_from");
            String dateTo = request.getParameter("date_to");
            String account_id = request.getParameter("account_id");
            ArrayList<HistoryUnit> history = null;
            if (dateFrom == null && dateTo==null)
            history = dbConnection.getAccountHistory(user_id, account_id);
            else if (dateFrom == null && dateTo != null)
            history = dbConnection.getAccountHistory(user_id, account_id,dateFrom);
            else if (dateFrom == null && dateTo != null)
            history = dbConnection.getAccountHistory(user_id, account_id,"1000-01-01",dateTo);
            else
            history = dbConnection.getAccountHistory(user_id, account_id,dateFrom,dateTo);%>
            <table class="table">
                <form method="post" action="account_history.jsp">
                    <table class="table">
                        <tr>
                            <td width="100"> Date from: </td>
                            <td width="100" align="center"> <input name="date_from"></td>
                            <td style="color: gray;" width="100" style="text-align:left;" > yyyy-mm-dd </td>
                        </tr>
                        <tr>
                            <td width="100"> Date to: </td>
                            <td width="100" align="center"> <input name="date_to"></td>
                            <td style="color: gray;" width="100" style="text-align:left;" > yyyy-mm-dd </td>
                        <tr>
                            <td></td><td><input type="submit" value="Submit"></td><td></td>
                        </tr>
                    </table>
                </form>

                        <table class="table" >
            <tr>
                <td width="200">Date</td>
                <td width="200">Operation name</td>
                <td width="100">Amount</td>
                <td width="300">Note</td>
            </tr>
            <% for (HistoryUnit historyUnit : history) { %>
                <tr>
                    <td><%= historyUnit.date%></td>
                    <td><%= historyUnit.operationName%></td>
                    <td <% if (historyUnit.money < 0) {%> style="color: red" <%} else {%> style="color: black;" <%}%>> <%=historyUnit.money%></td>
                    <td><%= historyUnit.note%></td>
                </tr>
            <% } %>
            <hr width="800" align="left"/>
        </table>
        <hr width="800" align="left"/>
    </div>
</div>
</body>
</html>